// pages/test/test.js
import { ajax, ROOT_URL } from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: ROOT_URL,
    testArr:[],
    testIndex:0,
    answerIndex:-1,
    answNum:'',
    answArr:[],
    isLwors:false,
    isSee: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    ajax('/index/Exam/exam_list', {
    }, res => {
      // console.log(res)
      if (res.status == 200) {
        _this.setData({
          testArr:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  //选择答案
  bindResult(e){
    let index = e.currentTarget.dataset.index;
    let answer = e.currentTarget.dataset.ansid;
    this.setData({
      answerIndex: index,
      answNum: answer,
      isLwors:true
    })
  },
  //点击下一题
  bindLower(e){
    let arr = this.data.answArr;
    let tstIndex = this.data.testIndex;
    let num = this.data.answNum;
    let anIndex = this.data.answerIndex;
    let length = this.data.testArr.length;

    tstIndex++;
    arr.push(num)
 
    if (tstIndex == length-1) {
      this.setData({
        isSee: false
      })
    } else if (tstIndex > length - 1){
      wx.redirectTo({
        url: '/pages/testResults/testResults?anArr='+arr,
      })
    }
    this.setData({
      testIndex: tstIndex,
      answArr: arr,
      answerIndex: -1,
      isLwors: false
    })
  },
  //返回上一页
  onRturn:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})